// extending gameClass in room Class
function roomClass(text_id, input_id, msg_id, button, trackid, no_submit) {
    gameClass.apply(this, [text_id, input_id, msg_id, button, trackid, no_submit]);
}
roomClass.prototype = new gameClass();
roomClass.prototype.constructor = roomClass;
roomClass.prototype.endGame = function() {
    this.handleProgress(true);
    this.showTimeInMessage();
    this.textObj.resetText();
    console.log("ending");
    $("#"+this.input_id).val("").attr("disabled", "disabled");
    this.timerObj.endTimer();
    this.status = "off";
};
roomClass.prototype.handleProgress = function(complete) {
    var progress;
    if(typeof complete!='undefined' && complete) {
        progress = 100;
    } else {
        progress = this.textObj.getProgress();
    }
    $('#'+this.trackid+"progress").css('width', progress+'%');
    roomData.p = progress;
    ajaxObj.updateProgress(progress);
}


function room_showPlayers() {
    var p = roomData.players, txt, allReady = true, count=0;
    $('#room_players').html("");
    for(i in p) {
        text = '<div class="largeText">'+p[i].username+' (<span class="smallText">'+(p[i].time_taken/10)+'s</span>) </div>';
        // user is ready
        if(p[i].completed !== null) {
            text += '<div id="paraMeta_'+p[i].id+'" class="paraMeta">'
            +' <div id="paraprogress_'+p[i].id+'" class="paraprogress" style="width:'+p[i].completed+'%">'
            +'</div></div>';
            if(p[i].username == roomData.username)
                $('#readyToPlay').hide();
            // user has already complted this track
            if(p[i].username == roomData.username && p[i].completed == '100')
                allReady = false;

        } else allReady = false;
        $('#room_players').append(text);
        count++;
    }
    if(count > 1 && allReady && !roomObj) {
        $('#roomMsg').text("");
        roomObj = new roomClass("para", "typeValue", "timeDiv", "gameHandle", "para");
        roomObj.initGame();
    }
}

function room_updateStatus(flag) {
    if(flag) $("#onlineStatus").text('online');
    else $("#onlineStatus").text('offline');
}


$(function() {
    //initiate long polling to read the status of the file
    ajaxObj.longPollRoom();     
    $('#readyToPlay').click(function() {
        $(this).hide();
        $('#roomMsg').text("waiting for other players!");
        ajaxObj.markReady();
    });
});

